Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mod_config): Allow for nested config values for mods #407

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

JayPaulinCodes
Copy link

The Goal

The goal of this PR is to allow for mods config.lua file to make use of nested values.

The Problem

Consider the following:
For sake of example, lets say I have the following as my config.lua file for my mod:

return {
    ["foo_group_1"] = {
        ["money"] = 10,
        ["enabled"] = true,
    },
    ["foo_group_2"] = {
        ["yellow"] = {
            ["label"] = "cat",
            ["enabled"] = false,
        },
    },
}

Lets say the player uses the mod config menu to edit the settings of the mod to make the following table be our current saved config for the mod:

{
    ["foo_group_1"] = {
        ["money"] = 23,
        ["enabled"] = true,
    },
    ["foo_group_2"] = {
        ["yellow"] = {
            ["label"] = "bear",
            ["enabled"] = false,
        },
    },
}

As of right now all is fine, however, lets say the mod developer wants to add a feature to their mod and needs to add a new item in foo_group_2, making the new config.lua file look like so:

return {
    ["foo_group_1"] = {
        ["money"] = 10,
        ["enabled"] = true,
    },
    ["foo_group_2"] = {
        ["yellow"] = {
            ["label"] = "cat",
            ["enabled"] = false,
        },
        ["green"] = {
            ["label"] = "dog",
            ["enabled"] = false,
        },
    },
}

The problem now happens if our player loads the new version of the mod with their old saved config on file, the SMODS.load_mod_config method will do a combination of the saved and default configs without taking into account any nesting, resulting in a loaded mod config of a table like this:

{
    ["foo_group_1"] = {
        ["money"] = 23,
        ["enabled"] = true,
    },
    ["foo_group_2"] = {
        ["yellow"] = {
            ["label"] = "bear",
            ["enabled"] = false,
        },
    },
}

This is now a problem because the player's game will crash when trying to access config.foo_group_2.green

The Proposed Solution

With this PR, the final combined table will instead look like this:

{
    ["foo_group_1"] = {
        ["money"] = 23,
        ["enabled"] = true,
    },
    ["foo_group_2"] = {
        ["yellow"] = {
            ["label"] = "bear",
            ["enabled"] = false,
        },
        ["green"] = {
            ["label"] = "dog",
            ["enabled"] = false,
        },
    },
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant